package cn.wolfcode.luowowo.comment.service.impl;

import cn.wolfcode.luowowo.comment.domain.CommunityAnswer;
import cn.wolfcode.luowowo.comment.domain.StrategyComment;
import cn.wolfcode.luowowo.comment.domain.TravelComment;
import cn.wolfcode.luowowo.comment.query.CommunityAnswerQueryObject;
import cn.wolfcode.luowowo.comment.repository.CommunityAnswerRepository;
import cn.wolfcode.luowowo.comment.service.ICommunityAnswerService;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.Date;
import java.util.List;

@Service
public class CommunityAnswerServiceImpl implements ICommunityAnswerService {

    @Autowired
    private CommunityAnswerRepository communityAnswerRepository;

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 保存回答
     * @param communityAnswer
     */
    public void save(CommunityAnswer communityAnswer) {

        if (communityAnswer.getId() != null){
            communityAnswerRepository.save(communityAnswer);
        }else {
            communityAnswer.setCreateTime(new Date());
            communityAnswerRepository.insert(communityAnswer);
        }



    }

    public Page queryPageForAnswer(CommunityAnswerQueryObject qo) {

        Page<CommunityAnswer> page = communityAnswerRepository.findByDetailId(qo.getDetailId(),
                                             PageRequest.of(qo.getCurrentPage() - 1,
                                            qo.getPageSize(), new Sort(Sort.Direction.DESC, "thumbupnum")));
        return page;
    }

    public List<CommunityAnswer> findByDetailId(Long id) {

        if (id != null) {
            Query query = new Query();
            query.addCriteria(Criteria.where("detailId").is(id));
            List<CommunityAnswer> answers = mongoTemplate.find(query, CommunityAnswer.class);
            return answers;
        }
        return null;
    }

    /**
     * 根据浏览次数排序获得最多数量的一个
     * @param id
     * @return
     */
    public CommunityAnswer findTopByDetailId(Long id) {

        if (id != null) {
            Query query = new Query();
            // //设置分页信息
            query.skip(0).limit(1);
            query.with(new Sort(Sort.Direction.DESC,"thumbupnum"));
            query.addCriteria(Criteria.where("detailId").is(id));
            List<CommunityAnswer> answers = mongoTemplate.find(query, CommunityAnswer.class);

            if (answers.size() > 0){
                return answers.get(0);
            }
        }
        return null;
    }

    @Override
    public CommunityAnswer selectById(String id) {

        if (id != null) {
            Query query = new Query();
            query.addCriteria(Criteria.where("id").is(id));
            List<CommunityAnswer> answers = mongoTemplate.find(query, CommunityAnswer.class);
            if (answers != null && answers.size() > 0){
                return answers.get(0);
            }
        }
        return null;
    }
}
